<%@ taglib prefix="t" uri="/easyui-tags" %>
<%@ page import="org.jeecgframework.web.util.StaticValue" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<html>
<head>
    <title>自定义报表</title>
    <t:base type="jquery,easyui,tools,DatePicker"></t:base>
    <link rel="stylesheet" href="plug-in/jquery/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="plug-in/jquery/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="plug-in/jquery/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="plug-in/tools/Transfer.js"></script>
    <style type="text/css">
        thead th {
            border-top: 1px solid #000000;
            border-left: 1px solid #000000;
            border-bottom: 1px solid #000000;
            border-collapse: collapse;
            padding: 5px;
        }

        td {
            white-space: nowrap;
        }

        select {
            width: 146px;
        }
    </style>
</head>
<script type="text/javascript">
    $(function () {
        $("#lienkedRota").val(${cbsCgreportHeadPage.lienkedRota});
        var monthArr = {};
        var temp = [];  //每行自定义条件对应的 id
        $('#tempDiv tr').each(function (i) {
            var months = [];// 遍历 tr，存放每个td的文本信息
            $(this).children('td').each(function (j) {  // 遍历 tr 的各个 td
                months.push($(this).text());
                if (j == 0) {
                    temp.push($(this).text())
                }
            });
            monthArr[i] = months;
        });
        resGeneration2(monthArr, temp, 1);
        showOption(monthArr);
        getTableByRota();
        setTimeout("getTableNames()", 300);
        setTimeout("chageCgreport()", 300);
    });
    var setting = {
        check: {
            enable: true
        }

    };

    function getTableNames() {
        var flag = true;
        var zNodes;
        var nameArry = [];  //关联业务array
        var reportName = $("#reportName").val();    //报表名称
        if (reportName == '' || reportName == null) {
            tip("报表名称不能为空！");
            flag = false;
            return false;
        }

        var rotaListIdName = $("#rotaListId").val();    //主业务
        if (rotaListIdName == '' && rotaListIdName.length == 0) {
            tip("请选择表");
            flag = false;
            return false;
        }
        nameArry.push(rotaListIdName);
        $("select[name = 'rotaListName1']").each(function () {
            if (nameArry.indexOf($(this).attr('selected', 'selected').val()) == -1) {
                nameArry.push($(this).attr('selected', 'selected').val());
            } else {
                tip("关联表业务不能相同！");
                flag = false;
                return false;
            }

        });

        if (flag) {
            $.ajax({
                url: "cbsCgreportHeadController.do?getzdNameForTableName",
                data: {"nameArray": nameArry},
                cache: false,
                traditional: true,
                dataType: 'json',
                type: 'POST',
                success: function (data) {
                    $("#select_left").hide();
                    var raMap = data.obj;
                    zNodes = raMap;
                    zNodes = eval(zNodes);  //序列化json数据
                    $.fn.zTree.init($("#treeDemo"), setting, zNodes);//初始化树
                }
            });
        }
    }

    /**
     * 添加附表
     */
    function addTables() {
        var html = "";
        var nodes = new Array();
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        var childNodes = treeObj.getCheckedNodes(true);

        for (var i = 0; i < childNodes.length; i++) {
            var flag = childNodes[i].isParent;
            if (flag == true) {
                continue;
            } else {
                nodes[i] = childNodes[i].id;
                // nodes[i] = childNodes[i].name;
                html += "<option value=" + childNodes[i].id + ">" + childNodes[i].name + "</option>";
            }

        }
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
        treeObj.checkAllNodes(false);
        $("#select_right").append(html);
    }

    /**
     * 已选项目回显
     */
    function showOption(arr) {
        var html = "";
        var flag = 'F';
        for (var i in arr) {
            switch (flag) {
                case 'F':
                    if (arr[i][6] == "1") {  //时间差查询
                        html += "<option value=" + arr[i][0] + ">" + arr[i][1] + "</option>";
                        flag = 'T';
                    } else {
                        flag = 'F';
                        html += "<option value=" + arr[i][0] + ">" + arr[i][1] + "</option>";
                    }
                    break;
                case 'T':
                    flag = 'F';
                    continue;
                    break;
            }
        }
        $("#select_right").append(html);
    }

    function moveTables() {
        $("#select_right").find("option:selected").remove()
    }

    /**
     * 根据字段类型，获取查询条件 ，回显时
     */
    function getTableByRota() {
        var id = $("#rotaListId").val();
        var lienkedRota = $("#lienkedRota").val();

        $.ajax({
            url: "cbsCgreportHeadController.do?getRotalevel",
            data: {
                "levelid": id
            },
            cache: false,
            dataType: 'json',
            type: 'POST',
            success: function (data) {
                if (data.success) {
                    var obj = data.obj;
                    if (lienkedRota.indexOf(',') == -1) {
                        var html = "<option value=''>-- 请选择 --</option>";
                        obj.forEach(function (i, item) {
                            if (i.rotaName == lienkedRota) {
                                html += '<option value="' + i.rotaName + '" selected="selected">' + i.rotaNameChinese + '</option>';
                            } else {
                                html += '<option value="' + i.rotaName + '">' + i.rotaNameChinese + '</option>';
                            }
                        });
                        $("select[name^='rotaListName1']").html(html);
                    } else {
                        var words = lienkedRota.split(',');
                        for (var j = 0; j < words.length; j++) {
                            if (j == 0) {  /*默认的第一个选择*/
                                var html = "<option value=''>-- 请选择 --</option>";
                                obj.forEach(function (i, item) {
                                    if (i.rotaName == words[j]) {
                                        html += '<option value="' + i.rotaName + '" selected="selected">' + i.rotaNameChinese + '</option>';
                                    } else {
                                        html += '<option value="' + i.rotaName + '">' + i.rotaNameChinese + '</option>';
                                    }
                                });
                                $("select[name^='rotaListName1']").html(html);
                            } else {    /*剩余复制的选择*/
                                $("#rotaSelect").append("<span><br/><br/>");
                                var oSel = document.getElementById('rotaListId1');
                                var oNewSel = oSel.cloneNode(true);
                                document.getElementById("rotaSelect").append(oNewSel);
                                obj.forEach(function (i, item) {
                                    if (i.rotaName == words[j]) {
                                        $(oNewSel).val(i.rotaName);
                                    }
                                });
                                $("#rotaSelect").append("<input type='button' value='删除' onclick='delTable(this)' style='margin-left: 5px'></span>");
                            }
                        }
                    }
                } else {
                    mui.alert(data.msg);
                }
            }
        });
    }
</script>
<body>
<input type="hidden" id="cgid" value="${cbsCgreportHeadPage.id}">
<table class="nobordertable noborder" style="border: none;">
    <tbody id="mtbody">
    <tr>
        <td class="al_r" width="20%">
            <label>报表名称<label style="color: red;">*</label>:</label>

        </td>
        <td width="30%">
            <input id="reportName" disabled="disabled" value="${cbsCgreportHeadPage.name}"
                   name="name"/>&nbsp&nbsp&nbsp<span id="isOrNotUse"></span>
        </td>
        <td class="al_r" width="20%">
            <label>报表说明:</label>
        </td>
        <td width="30%" rowspan="2">
            <textarea style="width: 100%;height: 60px;" id="content">${cbsCgreportHeadPage.content}</textarea>
        </td>
    </tr>
    <tr>
        <td class="al_r" width="20%">
        </td>
        <td colspan="3" width="80%"><span style="font-size:3px;">不能含有以下特殊字符：@/'"#$%&^*!￥:;.+=()<>、</span></td>
    </tr>
    <tr>
        <td class="al_r" width="20%">
            <label>选择主业务<label style="color: red;">*</label>:</label>
        </td>
        <td width="30%">
            <select name="rotaListName" id="rotaListId"
                    onchange="changeRotaLevel(this.options[this.options.selectedIndex].value)">
                <option value="">-- 请选择 --</option>
                <c:forEach items="${cbsRotaLevelList}" var="rota" varStatus="v">
                    <option value="${rota.id}"
                            <c:if test="${cbsCgreportHeadPage.primatyRota == rota.id}">selected="selected"</c:if>>${rota.rotaNameChinese}</option>
                </c:forEach>
            </select>
        </td>
        <td class="al_r" width="20%">

        </td>
    </tr>
    <tr>
        <td class="al_r" width="20%">
            <label>选择关联业务:</label>
            <input type="hidden" id="lienkedRota"/>
        </td>
        <td width="30%">
            <div id="rotaSelect">
                <select name="rotaListName1" id="rotaListId1">
                    <option value="">-- 请选择 --</option>
                </select>
                <input type="button" value="添加" onclick="addTable()">
            </div>
        </td>
        <td class="al_r" width="20%">
            <label>是否共享:</label>
        </td>
        <td width="30%">
            <input type="checkbox" name="gx" value="${cbsCgreportHeadPage.property}"
                   <c:if test="${cbsCgreportHeadPage.property == '1'}">checked="checked"</c:if>/>
        </td>
    </tr>
    </tbody>
</table>
<div style="margin: 10px auto; text-align: center;">
    <a href="#" class="easyui-linkbutton l-btn" onclick="getTableNames('update')">生成待选</a>
    <a href="#" class="easyui-linkbutton l-btn" onclick="saveRoteLevel('update')">保存</a>
    <a href="#" class="easyui-linkbutton l-btn" onclick="seeResult('update')">预览结果</a>
</div>
<div class="line"></div>
<form id="myform" name="myform" method="post">
    <table style="width: 100%;">
        <tbody>
        <tr>
            <td width="40%" valign="top">
                <table align="center" width="100%" border="0">
                    <tr>
                        <td width="80" align="center">
                            <div class="subtitle" style="border: 0px;">
                                <p>待选项目</p>
                            </div>
                            <div class="ty-transfer-list-body"
                                 style="border: 1px solid #DCDCDC;overflow:scroll;height: 390px">
                                <ul id="treeDemo" class="ztree">
                                </ul>
                            </div>
                        </td>
                        <td width="10" align="center">
                            <input type="button" value="添加"
                                   onclick="addTables()"><br><br>
                            <input type="button" value="删除"
                                   onclick="moveTables()">
                        </td>
                        <td width="80" align="center">
                            <div class="subtitle" style="border: 0px;">
                                <p>已选项目</p>
                            </div>
                            <div style="height: 390px;">
                                <select style="width:100%;height: 390px;overflow:auto;" multiple id="select_right"
                                        name="select_right"
                                        ondblclick="moveOption(this, document.getElementById('select_right'))">
                                </select>
                            </div>
                        </td>
                        <td width="10" align="center">
                            <INPUT TYPE="button" value="↑"
                                   onclick="moveUp(document.getElementById('select_right'))"><br>
                            <div style="height: 10px;"></div>
                            <INPUT TYPE="button" value="↓" onclick="moveDown(document.getElementById('select_right'));"><br>

                        </td>
                    </tr>
                </table>
                <div align="center"><input type="hidden" id="city" name="city" size="23"/></div>
            </td>
            <td width="60%" valign="top">
                <div class="subtitle" style="border: 0px;margin-bottom: 10px;margin-left:70%">
                    <a href="#" class="easyui-linkbutton l-btn" onclick="resGeneration()">生成自定义条件</a>
                </div>
                <div class="sub-div" style="width:800px; overflow:scroll;">
                    <fieldset style="border:1px #cccccc solid">
                        <legend>自定义条件</legend>
                        <table style="width: 100%;overflow:scroll;">
                            <thead style="border: 1px solid #000000;">
                            <th>查询项目名称</th>
                            <th>查询条件</th>
                            <th>查询内容</th>
                            <th>是否查询条件</th>
                            <th>组合条件</th>
                            <th>时间差查询</th>
                            </thead>
                            <tbody id="Ttbody">

                            </tbody>
                        </table>
                    </fieldset>
                </div>
                <div>
                    <table id="tempDiv" hidden="hidden">
                        <c:forEach items="${maps}" var="map">
                            <tr>
                                <td>${map.id}</td>
                                <td>${map.field_chinese}</td>
                                <td>${map.querycriteria}</td>
                                <td>${map.content}</td>
                                <td>${map.isgroup}</td>
                                <td>${map.condition}</td>
                                <td>${map.timequery}</td>
                                <td>${map.tabname}</td>
                            </tr>
                        </c:forEach>
                    </table>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
</form>
</body>
<script src="webpage/com/zybk/cbscgreporthead/cbsCgreportHead.js"></script>
</html>
